package com.asen.commons.handler;

import com.asen.commons.exception.ServiceException;
import com.asen.commons.utils.ViewDisplayParser;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 处理返回页面的显示属性
 *
 * @author Gary
 * @version 1.0
 * @date 2019-07-09
 */
@Component
@Aspect
@Slf4j
@Order(-1)
public class ViewRevisionAspect {

    @Value("${scp.performance.maxOverCost:1000}")
    private long maxOverCost;

    private static final String LOGIN_CONTROLLER = "com.cis.scp.sys.controller.LoginController";
    private static final String[] LOGIN_METHOD = {"auth", "logout"};

    @Pointcut("execution(* com.asen..controller.*Controller.*(..)) ")
    public void pointcutMethod() {

    }

    private boolean isIgnoreAspect(JoinPoint point) {
        String className = point.getTarget().getClass().getName();
        String methodName = point.getSignature().getName();

        //if (LOGIN_CONTROLLER.equals(className)
        //        && (StringUtils.containsAny(methodName, LOGIN_METHOD))) {
        //    return true;
        //}
        return false;
    }

    @Around("pointcutMethod()")
    public Object aroundCon(ProceedingJoinPoint point) throws Throwable {
        log.info("显示属性处理开始：");
        Object object = point.proceed();
        if (object == null) {
            return object;
        }
        if (isIgnoreAspect(point)) {
            return object;
        }

        long begin = System.currentTimeMillis();
        try {
            //协同方跨租户处理
            //if (UserUtils.isLoginByPortal() && UserUtils.getPortalTenantId() != null) {
            //    Object tenantObject = object;
            //    object = SysUtils.callAt(UserUtils.getPortalTenantId(), () -> ViewDisplayParser.parse(tenantObject));
            //} else {
                object = ViewDisplayParser.parse(object);
            //}
        } finally {
            long cost = System.currentTimeMillis() - begin;
            log.info("显示属性处理耗时：{}毫秒", cost);
            if (cost > maxOverCost) {
                String className = point.getTarget().getClass().getName();
                String methodName = point.getSignature().getName();
                StringBuilder sb = new StringBuilder();
                sb.append(className);
                sb.append(".");
                sb.append(methodName);
                sb.append("(");
                if (point.getArgs() != null && point.getArgs().length > 0) {
                    int i = 0;
                    for (Object o : point.getArgs()) {
                        if (i > 0) {
                            sb.append(",");
                        }
                        sb.append(o);
                        i++;
                    }
                }
                sb.append(")");
                log.info("{} ==> 显示属性处理耗时：{}毫秒", sb.toString(), cost);
            }
        }

        return object;
    }
}
